package com.lyf.lottery.service.impl;


import com.lyf.common.api.CommonResult;
import com.lyf.lottery.constant.Const;
import com.lyf.lottery.dao.mapper.OptActivityBlindBoxMapper;
import com.lyf.lottery.model.po.OptActivity;
import com.lyf.lottery.model.po.OptActivityBlindBox;
import com.lyf.lottery.model.req.OptActivityBlindBoxQuery;
import com.lyf.lottery.service.OptActivityBlindBoxService;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.io.Serializable;

import static com.lyf.lottery.common.LotteryResultCode.DATA_NOT_EXIST;
import static com.lyf.lottery.common.LotteryResultCode.PROBABILITY_GTE_ONE;
import static com.lyf.lottery.constant.Const.ActivityStatusEnum.PUT_DOWN;
import static com.lyf.lottery.constant.Const.ActivityStatusEnum.PUT_ON;

/**
 * OptActivityBlindBox service
 * @author lyf
 **/
@Slf4j
@Service
public class OptActivityBlindBoxServiceImpl implements OptActivityBlindBoxService {

    @Resource
    private  OptActivityBlindBoxMapper optActivityBlindBoxMapper;

    /**
     * 分页查询
     * @param query 查询条件
     * @date 2020/12/25
     * @return PageInfo
     */
    @Override
    public PageInfo<OptActivityBlindBox> selectPage(OptActivityBlindBoxQuery query){
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<OptActivityBlindBox> list = optActivityBlindBoxMapper.selectList(null);
        return new PageInfo<>(list);
    }

    /**
     * 插入数据
     * @param entity entity
     * @date 2020/12/25
     * @return com.lyf.lottery.entity.OptActivityBlindBox
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OptActivityBlindBox insert(OptActivityBlindBox entity){
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setStatus(PUT_DOWN.getCode());
        entity.setObtainNum(Const.CONST_0);
        optActivityBlindBoxMapper.insert(entity);
        return entity;
     }

    /**
     * 修改数据
     * @param entity entity
     * @date 2020/12/25
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(OptActivityBlindBox entity){
        optActivityBlindBoxMapper.updateById(entity);
    }

    @Override
    public CommonResult putOn(Integer id) {
        OptActivityBlindBox optActivityBlindBox = optActivityBlindBoxMapper.selectById(id);
        if(null == optActivityBlindBox){
            return CommonResult.failed(DATA_NOT_EXIST);
        }
        optActivityBlindBox.setStatus(PUT_ON.getCode());
        optActivityBlindBox.setUpdateTime(new Date());
        BigDecimal bigDecimal = optActivityBlindBoxMapper.optActivityBlindBox(optActivityBlindBox.getActivityId());
        BigDecimal decimal = bigDecimal.add(optActivityBlindBox.getProbability());
        if(decimal.compareTo(new BigDecimal(1)) > 0){
            return CommonResult.failed(PROBABILITY_GTE_ONE);
        }
        optActivityBlindBoxMapper.updateById(optActivityBlindBox);
        return CommonResult.success(true);
    }

    @Override
    public CommonResult putDown(Integer id) {
        OptActivityBlindBox optActivityBlindBox = optActivityBlindBoxMapper.selectById(id);
        if(null == optActivityBlindBox){
            return CommonResult.failed(DATA_NOT_EXIST);
        }
        optActivityBlindBox.setStatus(PUT_DOWN.getCode());
        optActivityBlindBox.setUpdateTime(new Date());
        optActivityBlindBoxMapper.updateById(optActivityBlindBox);
        return CommonResult.success(true);
    }
}
